

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Spacy-NP Annotator &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
        <script type="text/javascript" src="_static/jquery.js"></script>
        <script type="text/javascript" src="_static/underscore.js"></script>
        <script type="text/javascript" src="_static/doctools.js"></script>
        <script type="text/javascript" src="_static/language_data.js"></script>
        <script type="text/javascript" src="_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="index.html">
          

          
            
            <img src="_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="transformers.html">Transformers</a></li>
<li class="toctree-l1"><a class="reference internal" href="archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="index.html">Docs</a> &raquo;</li>
        
      <li>Spacy-NP Annotator</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="spacy-np-annotator">
<h1>Spacy-NP Annotator<a class="headerlink" href="#spacy-np-annotator" title="Permalink to this headline">¶</a></h1>
<div class="section" id="chunker-based-noun-phrase-annotator">
<h2>Chunker based noun phrase annotator<a class="headerlink" href="#chunker-based-noun-phrase-annotator" title="Permalink to this headline">¶</a></h2>
<p>The noun phrase annotator is a plug-in that can be used with <a class="reference external" href="https://spacy.io">Spacy</a> pipeline structure.</p>
<p>The annotator loads a trained <a class="reference internal" href="generated_api/nlp_architect.models.html#nlp_architect.models.chunker.SequenceChunker" title="nlp_architect.models.chunker.SequenceChunker"><code class="xref py py-class docutils literal notranslate"><span class="pre">SequenceChunker</span></code></a> model that is able to predict chunk labels, creates Spacy based Span objects and applies a sequence of filtering to produce a set of noun phrases, finally, it attaches it to the document object.</p>
<p>The annotator implementation can be found in <a class="reference internal" href="generated_api/nlp_architect.pipelines.html#nlp_architect.pipelines.spacy_np_annotator.NPAnnotator" title="nlp_architect.pipelines.spacy_np_annotator.NPAnnotator"><code class="xref py py-class docutils literal notranslate"><span class="pre">NPAnnotator</span></code></a>.</p>
<div class="section" id="usage-example">
<h3>Usage example<a class="headerlink" href="#usage-example" title="Permalink to this headline">¶</a></h3>
<p>Loading a Spacy pipeline and adding a sentence breaker (required) and <a class="reference internal" href="generated_api/nlp_architect.pipelines.html#nlp_architect.pipelines.spacy_np_annotator.NPAnnotator" title="nlp_architect.pipelines.spacy_np_annotator.NPAnnotator"><code class="xref py py-class docutils literal notranslate"><span class="pre">NPAnnotator</span></code></a> annotator as the last annotator in the pipeline:</p>
<div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">nlp</span> <span class="o">=</span> <span class="n">spacy</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s1">&#39;en&#39;</span><span class="p">)</span>
<span class="n">nlp</span><span class="o">.</span><span class="n">add_pipe</span><span class="p">(</span><span class="n">nlp</span><span class="o">.</span><span class="n">create_pipe</span><span class="p">(</span><span class="s1">&#39;sentencizer&#39;</span><span class="p">),</span> <span class="n">first</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">nlp</span><span class="o">.</span><span class="n">add_pipe</span><span class="p">(</span><span class="n">NPAnnotator</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="o">&lt;</span><span class="n">path_to_model</span><span class="o">&gt;</span><span class="p">,</span> <span class="o">&lt;</span><span class="n">path_to_params</span><span class="o">&gt;</span><span class="p">),</span> <span class="n">last</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Parse documents regularly and get the noun phrase annotations using a dedicated method:</p>
<div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">doc</span> <span class="o">=</span> <span class="n">nlp</span><span class="p">(</span><span class="s1">&#39;The quick brown fox jumped over the fence&#39;</span><span class="p">)</span>
<span class="n">noun_phrases</span> <span class="o">=</span> <span class="n">nlp_architect</span><span class="o">.</span><span class="n">pipelines</span><span class="o">.</span><span class="n">spacy_np_annotator</span><span class="o">.</span><span class="n">get_noun_phrases</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="standalone-spacy-npannotator">
<h2>Standalone Spacy-NPAnnotator<a class="headerlink" href="#standalone-spacy-npannotator" title="Permalink to this headline">¶</a></h2>
<p>For use cases in which the user is not interested in specialized Spacy pipelines we have implemented <a class="reference internal" href="generated_api/nlp_architect.pipelines.html#nlp_architect.pipelines.spacy_np_annotator.SpacyNPAnnotator" title="nlp_architect.pipelines.spacy_np_annotator.SpacyNPAnnotator"><code class="xref py py-class docutils literal notranslate"><span class="pre">SpacyNPAnnotator</span></code></a> which will run a Spacy pipeline internally and provide string based noun phrase chunks given documents in string format.</p>
<div class="section" id="id1">
<h3>Usage example<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<p>Just as in <a class="reference internal" href="generated_api/nlp_architect.pipelines.html#nlp_architect.pipelines.spacy_np_annotator.NPAnnotator" title="nlp_architect.pipelines.spacy_np_annotator.NPAnnotator"><code class="xref py py-class docutils literal notranslate"><span class="pre">NPAnnotator</span></code></a>, we need to provide a trained <a class="reference internal" href="generated_api/nlp_architect.models.html#nlp_architect.models.chunker.SequenceChunker" title="nlp_architect.models.chunker.SequenceChunker"><code class="xref py py-class docutils literal notranslate"><span class="pre">SequenceChunker</span></code></a> model and its parameters file. It is also possible to provide a specific Spacy model to base the pipeline on.</p>
<p>The following example shows how to load a model/parameters using the default Spacy English model (<cite>en</cite>) and how to get the noun phrase annotations.</p>
<div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">spacy_np</span> <span class="o">=</span> <span class="n">SpacyNPAnnotator</span><span class="p">(</span><span class="o">&lt;</span><span class="n">model_path</span><span class="o">&gt;</span><span class="p">,</span> <span class="o">&lt;</span><span class="n">model_parameters_path</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">spacy_mode</span><span class="o">=</span><span class="s1">&#39;en&#39;</span><span class="p">)</span>
<span class="n">noun_phrases</span> <span class="o">=</span> <span class="n">spacy_np</span><span class="p">(</span><span class="s1">&#39;The quick brown fox jumped over the fence&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>